package com.cpi.cornpp.futurepurchase.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.customer.exception.CustomerException;
import com.cpi.cornpp.customer.services.CustomerServices;
import com.cpi.cornpp.customer.view.CustomerView;
import com.cpi.cornpp.futurepurchase.form.FuturePurchaseForm;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseByCustomerView;
import com.cs.common.utils.NumberUtil;

/**
 * 
 * @author Edgar Joao
 *
 */
public class CustomerListAction extends MappingDispatchAction {
	/** Identifier default workflow*/
	private final static String DEFAULT = "default";//$NON-NLS-1$
	/** Identifier fail workflow*/
	private static final String FAILURE = "fail";
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$		
	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private final static String CUSTOMER_ID_LABEL = "customerId";//$NON-NLS-1$
	private final static String CONTRACT_TYPE_LABEL = "contractType";//$NON-NLS-1$
	private final static String CONTRACT_NUMBERS_LABEL = "contratNumbers";//$NON-NLS-1$
	private final static String ACCOUNT_ID_LABEL = "accountId";//$NON-NLS-1$
	private final static String APPLICATION_ID_LABEL = "applicationId";//$NON-NLS-1$
	private final static String PRICE_LABEL = "price";//$NON-NLS-1$
	private final static String PURCHASE_DATE_LABEL = "purchaseDate";//$NON-NLS-1$
	private final static String REFERENCE_LABEL = "reference";//$NON-NLS-1$
	
	private final static String FUTURE_IN_SESSION_LABEL = "FuturePurchaseByCustomerView";//$NON-NLS-1$
	private static final String MESSAGE_SELECT_A_CLIENT = "error.message.futurepurchase.add.customers";//$NON-NLS-1$
	
	private void saveFutureInSession(HttpServletRequest request){
		HttpSession httpSession = null;
		FuturePurchaseByCustomerView futurePurchaseByCustomerView = null;
		if( request.getParameter(ACCOUNT_ID_LABEL) != null ){
			httpSession = request.getSession();
			futurePurchaseByCustomerView = new FuturePurchaseByCustomerView();
			futurePurchaseByCustomerView.setAccountPeriod(NumberUtil.getInt(request.getParameter(ACCOUNT_ID_LABEL)));
			futurePurchaseByCustomerView.setApplicationPeriod(NumberUtil.getInt(request.getParameter(APPLICATION_ID_LABEL)));
			futurePurchaseByCustomerView.setDate(request.getParameter(PURCHASE_DATE_LABEL));
			futurePurchaseByCustomerView.setDealNumber(request.getParameter(CONTRACT_NUMBERS_LABEL));
			futurePurchaseByCustomerView.setDealType(request.getParameter(CONTRACT_TYPE_LABEL));
			futurePurchaseByCustomerView.setPrice(request.getParameter(PRICE_LABEL));
			futurePurchaseByCustomerView.setReference(request.getParameter(REFERENCE_LABEL));
			
			httpSession.setAttribute(FUTURE_IN_SESSION_LABEL, futurePurchaseByCustomerView);
		}
	}
	/**
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goCustomerSelection(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws CustomerException {
		FuturePurchaseForm futurePurchaseForm = null;
		CustomerView customerView = null;
		String customerId = null;
		MessageResources messageResources = null;
		String messageError = null;
		HttpSession httpSession = null;
		CustomerServices customerServices = null;
		Collection listOfCustomers = null;
		try{
			listOfCustomers = new ArrayList(0);
			customerServices = new CustomerServices();
			httpSession = request.getSession();
			messageResources = getResources(request);
			futurePurchaseForm = (FuturePurchaseForm) form;
			this.saveFutureInSession(request);
			customerId = request.getParameter(CUSTOMER_ID_LABEL);
			customerView = new CustomerView();
			customerView.setCustomerId(StringUtils.EMPTY);
			if( StringUtils.isEmpty(customerId) ){
				customerView.setDescription(StringUtils.EMPTY);
				futurePurchaseForm.setCustomerViewSearch(customerView);
			}else{
				customerView.setDescription(customerId);
				futurePurchaseForm.setCustomerViewSearch(customerView);
				listOfCustomers = new ArrayList();
				listOfCustomers =  customerServices.getCustomersList(futurePurchaseForm.getCustomerViewSearch());
				request.setAttribute("listOfCustomers", listOfCustomers);
				futurePurchaseForm.setResultCustomerViews(listOfCustomers);
			}
		}catch (CustomerException customerException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), customerException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, customerException);
				form.reset(mapping, request);
				throw customerException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
			throw customerException;
		}
		catch (Exception e){
			CustomerException customerException=null;
			e.printStackTrace(System.out);
			customerException= new CustomerException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw customerException;
		}
		return mapping.findForward(DEFAULT);
	}
	/**
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward customerFutureSearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws CustomerException {

		FuturePurchaseForm futurePurchaseForm = null;		
		CustomerServices customerServices = null;
		Collection listOfCustomers = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;		
		
		try{
			httpSession = request.getSession();
			messageResources = getResources(request);
			futurePurchaseForm = (FuturePurchaseForm) form;
			customerServices = new CustomerServices();
			
			listOfCustomers = new ArrayList();
			listOfCustomers =  customerServices.getCustomersList(futurePurchaseForm.getCustomerViewSearch());
			request.setAttribute("listOfCustomers", listOfCustomers);
			futurePurchaseForm.setResultCustomerViews(listOfCustomers);
			
		}catch (CustomerException customerException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), customerException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, customerException);
				form.reset(mapping, request);
				throw customerException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
			throw customerException;
		}
		catch (Exception e){
			CustomerException customerException=null;
			e.printStackTrace(System.out);
			customerException= new CustomerException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw customerException;
		}
		form.reset(mapping, request);										
		return mapping.findForward(DEFAULT);
	}
	/**
	 * Metodo para seleccionar
	 * un cliente de la lista
	 * 
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward selectCustomer(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws CustomerException {
		
		FuturePurchaseForm futurePurchaseForm = null;
		CustomerView customerView = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		String forward = null;
		CustomerServices customerServices = null;
		try{
			messageResources = getResources(request);
			httpSession = request.getSession(false);
			futurePurchaseForm = (FuturePurchaseForm) form;
			
			if(futurePurchaseForm.getCustomerViewSearch().getCustomerId() != null){								
				if (!CollectionUtils.isEmpty(futurePurchaseForm.getResultCustomerViews())){
					Iterator iterator = futurePurchaseForm.getResultCustomerViews().iterator();
					while (iterator.hasNext()) {
						CustomerView view = (CustomerView) iterator.next();
						if(org.apache.commons.lang.StringUtils.equals(view.getCustomerId().trim(), futurePurchaseForm.getCustomerViewSearch().getCustomerId().trim())){
							customerView = new CustomerView();
							customerView.setCustomerId(view.getCustomerId());
							customerView.setDescription(view.getDescription());
							httpSession = request.getSession(false);
							httpSession.setAttribute("CustomerView", customerView);
							forward = DEFAULT;
							break;
						}
					}
				}else{
					customerServices = new CustomerServices();
					customerView = customerServices.getCustomerById(futurePurchaseForm.getCustomerViewSearch().getCustomerId());
					httpSession.setAttribute("CustomerView", customerView);
					forward = DEFAULT;
				}
			}else{
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), MESSAGE_SELECT_A_CLIENT);
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAILURE;
			}
						
		}catch (Exception e){
			CustomerException customerException=null;
			e.printStackTrace(System.out);
			customerException= new CustomerException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw customerException;
		}
		form.reset(mapping, request);										
		return mapping.findForward(forward);
	}
	
}
